package com.jtjrenren.android.taxi.driver.core.user.register;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.jtjrenren.android.taxi.driver.R;

import com.jtjrenren.android.taxi.driver.base.MvpActivity;
import com.library.utils.LogUtil;
import com.library.weiget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/27.
 */
public class UserRegisterActivity extends MvpActivity<RegisterPresenter> implements RegisterView {
    private static final int INTENT_CODE_CAMERA = 0;
    private static final int INTENT_CODE_IMGS = 1;
    private static final int INTENT_CODE_USE_CROP = 2;

    @BindView(R.id.register_channel_setp1)
    TextView tv_step1;
    @BindView(R.id.register_channel_setp2)
    TextView tv_step2;
    @BindView(R.id.register_channel_setp3)
    TextView tv_step3;
    @BindView(R.id.register_setp1)
    LinearLayout ll_step1;
    @BindView(R.id.register_setp2)
    LinearLayout ll_step2;
    @BindView(R.id.register_setp3)
    LinearLayout ll_step3;

    @BindView(R.id.register_step1_phone)
    ClearEditText et_phone;
    @BindView(R.id.register_step1_yzm)
    ClearEditText et_yzm;
    @BindView(R.id.register_step1_pwd)
    ClearEditText et_pwd;
    @BindView(R.id.register_step1_getyzm)
    Button bt_getyzm;
    @BindView(R.id.register_step1_tyxy)
    CheckBox cb_xy;
    @BindView(R.id.register_step1_syxy)
    TextView tv_xy;

    @BindView(R.id.register_step2_name)
    ClearEditText et_name;
    @BindView(R.id.register_step2_yybh)
    ClearEditText et_yybh;
    @BindView(R.id.register_step2_carcode)
    ClearEditText et_carcode;
    @BindView(R.id.register_step2_sfz)
    ClearEditText et_sfzh;
    @BindView(R.id.register_step2_name_desc)
    TextView tv_name;
    @BindView(R.id.register_step2_yybh_desc)
    TextView tv_yybh;
    @BindView(R.id.register_step2_carcode_desc)
    TextView tv_carcode;
    @BindView(R.id.register_step2_sfz_desc)
    TextView tv_sfzh;
    @BindView(R.id.register_step2_icon)
    ImageView iv_icon;
    @BindView(R.id.register_step2_companys)
    Spinner spinner_company;

    @BindView(R.id.register_step3_img)
    ImageView iv_yyz;

    private CountDownTimer countDownTimer = new CountDownTimer(30 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            bt_getyzm.setEnabled(false);
            bt_getyzm.setText(millisUntilFinished / 1000 + getString(R.string.miao));
        }

        @Override
        public void onFinish() {
            bt_getyzm.setEnabled(true);
            bt_getyzm.setText(getString(R.string.huoquyanzhengma));
        }
    };

    private Uri photoUri;
    private Bitmap bitmap;
    private ArrayAdapter<String> companyListAdapter;

    private int current_step = 0;

    @Override
    public int getLayoutID() {
        return R.layout.aty_user_register;
    }

    @Override
    public void initM() {
        presenter.init();
        setListeners();
        ((TextView) findViewById(R.id.title_title)).setText("注册");
        showStep1();
    }


    @Override
    protected void onDestroy() {
        LogUtil.log("register","onDestroy");
        super.onDestroy();
        presenter.destory();
    }

    @OnClick(R.id.title_back)
    public void back() {
        onBackPressed();
    }

    @OnClick(R.id.register_step1_getyzm)
    public void getYzmStep_1() {
        presenter.getYzm(et_phone.getText().toString().trim());
    }

    @OnClick(R.id.register_step1_next)
    public void next_1() {
        String yzm = et_yzm.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();
        boolean isChecked = cb_xy.isChecked();

        presenter.doStep1(yzm, pwd, isChecked);
    }

    @OnClick(R.id.register_step1_syxy)
    public void syxy_1() {

    }

    @OnClick(R.id.register_step2_icon_setting)
    public void setIcon_2() {
        showChooseImgDlg();
    }

    @OnClick(R.id.register_step2_next)
    public void next_2() {
        String name = et_name.getText().toString().trim();
        String yybh = et_yybh.getText().toString().trim();
        String carcode = et_carcode.getText().toString().trim();
        String sfzh = et_sfzh.getText().toString().trim();
        int companyPos = spinner_company.getSelectedItemPosition();
        presenter.doStep2(name, yybh, carcode, sfzh, companyPos, bitmap);
    }

    @OnClick(R.id.register_step3_pz)
    public void pz_3() {
        showChooseImgDlg();
    }

    @OnClick(R.id.register_step3_commit)
    public void commit_3() {
        presenter.doStep3(bitmap);
    }


    @Override
    public RegisterPresenterImpl createPresenter() {
        return new RegisterPresenterImpl(UserRegisterActivity.this);
    }

    @Override
    public void showTip(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoadding(String msg) {
        showProDialog("", msg);
    }

    @Override
    public void hideLoadding() {
        disProDialog();
    }

    @Override
    public void getYzmStart() {
        countDownTimer.start();
    }

    @Override
    public void refreshCompanyList(String[] companys) {
        companyListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, companys);
        companyListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_company.setAdapter(companyListAdapter);
    }

    @Override
    public void showStep1() {
        clearView();
        tv_step1.setBackgroundResource(R.mipmap.def_register_now);
        ll_step1.setVisibility(View.VISIBLE);

        current_step = 1;
    }

    @Override
    public void showStep2() {
        clearView();
        tv_step2.setBackgroundResource(R.mipmap.def_register_now);
        ll_step2.setVisibility(View.VISIBLE);

        current_step = 2;
    }

    @Override
    public void showStep3() {
        clearView();
        tv_step3.setBackgroundResource(R.mipmap.def_register_now);
        ll_step3.setVisibility(View.VISIBLE);

        current_step = 3;
    }

    @Override
    public void registerSucess() {
        finish();
    }

    private void clearView() {
        if (bitmap != null)
            bitmap.recycle();
        bitmap = null;

        ll_step1.setVisibility(View.GONE);
        ll_step2.setVisibility(View.GONE);
        ll_step3.setVisibility(View.GONE);

        tv_step1.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        tv_step2.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        tv_step3.setBackgroundColor(getResources().getColor(android.R.color.transparent));

    }


    private void showChooseImgDlg() {
        AlertDialog dialog = new AlertDialog.Builder(this).setItems(
                new String[]{"相机", "相册"},
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            dialog.dismiss();
                            Intent intent = new Intent(
                                    MediaStore.ACTION_IMAGE_CAPTURE);
                            ContentValues values = new ContentValues();
                            photoUri = getContentResolver()
                                    .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                            values);
                            intent.putExtra(
                                    android.provider.MediaStore.EXTRA_OUTPUT,
                                    photoUri);
                            startActivityForResult(intent, INTENT_CODE_CAMERA);
                        } else {
                            dialog.dismiss();

                            Intent i = new Intent("android.intent.action.PICK");
                            i.setDataAndType(
                                    MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                                    "image/*");

                            startActivityForResult(i, INTENT_CODE_IMGS);
                        }
                    }
                }).create();
        dialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                switch (requestCode) {
                    case INTENT_CODE_CAMERA:
                        startPhotoZoom(photoUri);
                        break;
                    case INTENT_CODE_IMGS:
                        photoUri = data.getData();
                        startPhotoZoom(photoUri);
                        break;
                    case INTENT_CODE_USE_CROP:
                        Bundle bundle = data.getExtras();
                        bitmap = (Bitmap) bundle.get("data");
                        if (current_step == 2)
                            iv_icon.setImageBitmap(bitmap);
                        else if (current_step == 3) {
                            iv_yyz.setImageBitmap(bitmap);
                            iv_yyz.setVisibility(View.VISIBLE);
                        }
                        break;
                }
                break;
        }
    }


    private void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");// crop为true是设置在开启的intent中设置显示的view可以剪裁
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, INTENT_CODE_USE_CROP);
    }

    private void setListeners() {

        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    tv_name.setVisibility(View.VISIBLE);
                } else {
                    tv_name.setVisibility(View.INVISIBLE);
                }
            }
        });
        et_yybh.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    tv_yybh.setVisibility(View.VISIBLE);
                } else {
                    tv_yybh.setVisibility(View.INVISIBLE);
                }
            }
        });
        et_carcode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    tv_carcode.setVisibility(View.VISIBLE);
                } else {
                    tv_carcode.setVisibility(View.INVISIBLE);
                }
            }
        });
        et_sfzh.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    tv_sfzh.setVisibility(View.VISIBLE);
                } else {
                    tv_sfzh.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
}
